﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Entity;
using HighLowBase.Entity;
namespace HighLowBase
{
    public class HiLoSession
    {
        public static SessionBaseEntity LoadSession(int gameId, int accountId)
        {
            var hiLoSession = DB.HighLowGameInstance.uspBaseHiLoSessionSelectBy(gameId, accountId).FirstOrDefault();
            if (hiLoSession == null)
                return null;

            return new SessionBaseEntity()
            {
                Id = hiLoSession.Id,
                AccountId = accountId,
                GameId = gameId,
                AuBet = hiLoSession.AuBet,
                AgBet = hiLoSession.AgBet,
                AuCurrent = hiLoSession.AuCurrent,
                AgCurrent = hiLoSession.AgCurrent,
                CurrentExtraLife = hiLoSession.CurrentExtraLife,
                CurrentOutOf = hiLoSession.CurrentOutOf,
                CurrentNudges = hiLoSession.CurrentNudges,
                CurrentFreeSpin = hiLoSession.CurrentFreeSpin,
                Round = hiLoSession.Round,
                Status = hiLoSession.Status
            };
        }

        //public static SupportedBaseEntity LoadSupported(int gameId, int accountId, long sessionId)
        //{
        //    var supported = DB.HighLowGameInstance.uspBaseHiLoSessionGetSupported(accountId, gameId, sessionId).FirstOrDefault();
        //    if (supported == null)
        //        return null;
        //    return new SupportedBaseEntity()
        //    {
        //        UsedExtraLife = supported.UsedExtraLife.GetValueOrDefault(0),
        //        UsedOutOf = supported.UsedOutOf.GetValueOrDefault(0),
        //        UsedNudges = supported.UsedNudges.GetValueOrDefault(0),
        //        UsedFreeSpin = supported.UsedFreeSpin.GetValueOrDefault(0),
        //        UsedCollect = supported.UsedCollect.GetValueOrDefault(0),
        //        Jackpot = supported.Jackpot.GetValueOrDefault(0)
        //    };
        //}

        public static SessionBaseEntity CreateSession(int accountId, string accountName, int accountTypeId, int hiLoGameId, int minimumValue, int maximumValue,
            int minimumBet, int maximumBet, double rate, double randomRate, int auBet, int agBet, int auCurrent, int agCurrent,
            int currentExtraLife, int currentOutOf, int currentNudges, int currentFreeSpin, int extraLife, int outOf, int nudges, int freeSpin,
            int maximumExtraLife, int maximumOutOf, int maximumNudges, int maximumFreeSpin, int round, int timeOut, int status)
        {
            long? id = 0;
            var sessionId = DB.HighLowGameInstance.uspBaseHiLoSesionNew(accountId, accountName, accountTypeId, hiLoGameId, minimumValue, maximumValue,
                minimumBet, maximumBet, rate, randomRate, auBet, agBet, auCurrent, agCurrent,
                currentExtraLife, currentOutOf, currentNudges, currentFreeSpin, extraLife, outOf, nudges, freeSpin, maximumExtraLife, maximumOutOf, maximumNudges, maximumFreeSpin,
                round, timeOut, status, ref id);

            if (sessionId > 0 && id.Value > 0)
            {
                return new SessionBaseEntity()
                {
                    Id = id.Value,
                    AccountId = accountId,
                    GameId = hiLoGameId,
                    AuBet = auBet,
                    AgBet = agBet,
                    AuCurrent = auCurrent,
                    AgCurrent = agCurrent,
                    CurrentExtraLife = currentExtraLife,
                    CurrentOutOf = currentOutOf,
                    CurrentNudges = currentNudges,
                    CurrentFreeSpin = currentFreeSpin,
                    Round = round,
                    Status = status
                };
            }
            else
            {
                throw new Exception(string.Format("Không thể bắt đầu game. (AccountId: {0}; GameId: {1}", accountId, hiLoGameId));
            }
        }


        public static int UpdateSession(long id, int accountId, string accountName, int accountTypeId, int hiLoGameId,
            double rate, double randomRate, int auBet, int agBet, int auCurrent, int agCurrent,
            int extraLifeType, int outOfType, int nudgesType, int freeSpinType, bool isJackpot, bool isCollect,
            int round, string description, int timeOut, int status)
        {
            return DB.HighLowGameInstance.uspBaseHiLoSessionUpdate(id, accountId, accountName, accountTypeId, hiLoGameId,
                rate, randomRate, auBet, agBet, auCurrent, agCurrent,
                extraLifeType, outOfType, nudgesType, freeSpinType, isJackpot, isCollect, round,
                description, timeOut, status);
        }

        public static int SubAuAgAccount(int accountId, int consumerId, int auSub, int agSub, string description)
        {
            return DB.PaymentInstance.uspAccountPlaySubAuAg(accountId, consumerId, auSub, agSub, description);
        }

        public static int AddAuAgAccount(int accountId, int consumerId, int auAdd, int agAdd, string description)
        {
            return DB.PaymentInstance.uspAccountPlayAddAuAg(accountId, consumerId, auAdd, agAdd, description);
        }
    }
}
